package logic

import (
	"context"
	"fmt"
	"projectes/models/mysql"

	"projectes/projectrpc/internal/svc"
	"projectes/projectrpc/projectrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SortAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSortAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SortAddLogic {
	return &SortAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SortAddLogic) SortAdd(in *projectrpc.SortAddRequest) (*projectrpc.SortAddResponse, error) {
	// todo: add your logic here and delete this line
	var products mysql.Sort
	mysql.DB.Debug().Model(&products).Where("product_name = ?", in.ProductName).Find(&products)

	product := mysql.Sort{

		Sort:        in.Sort,
		ProductCode: in.ProductCode,
		ProductName: in.ProductName,
		Number:      int(in.Number),
		Price:       float64(in.Price),
		Cost:        int(in.Cost),
		Img:         in.Img,
		UserId:      int(in.UserId),
	}

	err := mysql.DB.Debug().Model(&product).Create(&product).Error

	if err != nil {
		return nil, fmt.Errorf("商品发布失败")
	}
	return &projectrpc.SortAddResponse{UserId: int64(product.UserId)}, nil
}
